﻿using System;
using System.Windows.Forms;

namespace Storm.TextEditor.Editor.CodeCompletion
{
    /// <summary>
    /// Represents the default completion data holder.
    /// </summary>
    public class DefaultCompletionData
        : ICompletionData
    {
        #region Fields

        private int imageIndex = 0;
        private string text = "";
        private string description = "";
        private object tag = null;

        #endregion

        #region Properties

        /// <summary>
        /// Whether this type of completion data has a custom insertion action.
        /// </summary>
        public bool CustomInsertionAction
        {
            get { return false; }
        }

        /// <summary>
        /// Gets or sets the index of the image.
        /// </summary>
        /// <value>The index of the image.</value>
        public int ImageIndex
        {
            get { return imageIndex; }
            set { imageIndex = value; }
        }

        /// <summary>
        /// Gets or sets the text.
        /// </summary>
        /// <value>The text.</value>
        public string Text
        {
            get { return text; }
            set { text = value; }
        }

        /// <summary>
        /// Gets or sets the description.
        /// </summary>
        /// <value>The description.</value>
        public string Description
        {
            get { return description; }
            set { description = value; }
        }

        /// <summary>
        /// Gets or sets the tag of this completion data.
        /// </summary>
        public object Tag
        {
            get { return tag; }
            set { tag = value; }
        }

        #endregion

        #region Methods

        #region Public

        /// <summary>
        /// Compares this instance to the given instance.
        /// </summary>
        /// <param name="obj">The instance to compare to.</param>
        /// <returns></returns>
        public int CompareTo(object obj)
        {
            if (obj is ICompletionData)
            {
                ICompletionData completionData = (ICompletionData)obj;
                return this.Text.CompareTo(completionData.Text);
            }
            else
                throw new ArgumentNullException("obj");
        }

        public static int Compare(ICompletionData a, ICompletionData b)
        {
            if (a == null)
                throw new ArgumentNullException("a");

            if (b == null)
                throw new ArgumentNullException("b");

            return string.Compare(a.Text, b.Text, StringComparison.InvariantCultureIgnoreCase);
        }

        /// <summary>
        /// Provides the ability to have a completion data type with a custom insertion action.
        /// </summary>
        /// <param name="x">The x to insert at.</param>
        /// <param name="y">The y to insert at.</param>
        /// <param name="textEditor">The text editor in which this completion data resides.</param>
        public void Insert(int x, int y, TextEditor textEditor)
        {
        }

        /// <summary>
        /// Provides the ability to have a completion data type that can decide when it wants to be completed.
        /// </summary>
        /// <param name="key">The key that the user has typed.</param>
        /// <returns>Whether the code completer should complete.</returns>
        public bool ShouldInsert(char key)
        {
            return true;
        }

        #endregion

        #endregion

        /// <summary>
        /// Initializes a new instance of the <see cref="DefaultCompletionData"/> class.
        /// </summary>
        public DefaultCompletionData()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DefaultCompletionData"/> class.
        /// </summary>
        /// <param name="imageIndex">Index of the image.</param>
        /// <param name="text">The text.</param>
        /// <param name="description">The description.</param>
        public DefaultCompletionData(int imageIndex, string text, string description)
        {
            this.ImageIndex = imageIndex;
            this.Text = text;
            this.Description = description;
        }
    }
}
